#!/usr/bin/python2.5

"""Creates a file with 200 most populated cities (cities_top_200.csv)
"""

count = 0
cities = []

for line in open('cities15000.txt'):
    a = line.split('\t')

    pop = int(a[14])

    # there are more than 300 cities with population more than 1 million
    # consider only them
    if pop > 1000000:

        count += 1

        city = {}
        city['lat'] = a[4]
        city['lon'] = a[5]
        city['pop'] = pop
        city['name'] = a[2]

        cities.append(city)

print "count = ",count

def compare_pop(c1,c2):
    "compare population of two cities (for sorting)"
    return cmp(c1['pop'],c2['pop'])

cities.sort(compare_pop,reverse=True)

file_top = open('cities_top_200.csv','w')
for c in cities[:200]:
    file_top.write("""%s,%s,"%s,%s"\n""" % (c['name'],c['pop'],c['lat'],c['lon']))
